<template>
	<view>
		<view class="info">
			<view class="infos">
				<view class="one">
					<view class="name">
						{{info.name}}
					</view>
					<view class="tel">
						{{info.curriculumName}}
					</view>
				</view>
				<view class="address">
					{{info.detailedAddress}}
				</view>
			</view>
		</view>
		<view class="list">
			<view class="list-item">
				<view class="item-title">
					订单金额:
				</view>
				<view class="item-text">
					{{info.costMoney}}元
				</view>
			</view>
			<view class="list-item">
				<view class="item-title">
					订单时间:
				</view>
				<view class="item-text">
					{{info.createTime}}
				</view>
			</view>
			<view class="list-item">
				<view class="item-title">
					手机号码:
				</view>
				<view class="item-text">
					{{info.phone}}
				</view>
			</view>
		</view>
		<view class="pay" @click="getPayInfo">
			立即支付
		</view>

	</view>
</template>

<script>
	import {
		memberCurriculumdetail,
		payment
	} from "@/api/home/index.js"
	export default {
		data() {
			return {
				info: {},
				id: '',
				userinfo: {}
			}
		},
		async onLoad(val) {
			this.userinfo = uni.getStorageSync('userInfo')
			console.log(this.userinfo, '用户信息');
			console.log(val.id);
			this.id = val.id
			let res = await memberCurriculumdetail(
				val
			)
			console.log(res.data, '1111111');
			this.info = res.data
		},
		methods: {
			// async pays() {
			// 	let res = await payment({
			// 		courseId: this.info.curriculumId,
			// 		name: this.info.name,
			// 		payType: 'curriculum'
			// 	})
			// 	console.log(res);
			// },
			// 生成订单并获取支付请求凭证
			async getPayInfo(code) {
				let res = await this.dispose()
				console.log('调用结果', res)
				this.pays(res)
			},


			// 调用发起小程序支付
			pays(data) {
				let that = this
				let orderId = this.orderId
				console.log(data,'4s5d45sa');
				uni.requestPayment({
					"provider": "wxpay",
					"appId": data.appId,
					"timeStamp": data.timeStamp,
					"nonceStr": data.nonceStr,
					"package": data.packageValue,
					"signType": data.signType,
					"paySign": data.paySign,
					success(res) {
						uni.showToast({
							title: '支付成功',
							duration: 3000,
							success() {
								if (uni.getStorageSync("paperId")) {
									setTimeout(() => {
										uni.showModal({
											title: '提示',
											content: '是否前往学前测试？',
											success: function(res) {
												if (res.confirm) {

													console.log('用户点击确定');
													uni.navigateTo({
														url: `/packageA/pages/exam/index?id=${that.id}`
													})
												} else if (res.cancel) {
													console.log(1222222)
													// uni.navigateTo({
													// 	url: `/packageA/pages/payok/payok?id=${that.id}`
													// })
													uni.reLaunch({
														url: `/packageA/pages/mybaoming/mybaoming`
													})

												}
											}
										})
									}, 1000)
								} else {
									uni.navigateTo({
										url: `/packageA/pages/mybaoming/mybaoming`
									})
								}
							}
						})
					},
					fail(err) {
						console.log(err);
						uni.showToast({
							title: '取消支付',
							duration: 3000,
							success() {
								console.log('999');
							}
						})
					}
				})
			},
			// 网络请求
			dispose() {
				return uni.request({
					url: `https://qyh.huidehuaxiang.com/tencent/payment`,
					// url: `http://192.168.3.101/biz/tencent/payment`,

					method: 'post',
					header: {
						'Authorization': 'PurchaseCourses ' + uni.getStorageSync('token')
					},
					data: {
						courseId: this.info.curriculumId,
						name: this.info.name,
						payType: 'curriculum'
					},
				}).then(data => {
					console.log(data,'jkjkjk')
					const [error, res] = data;

					// 出错
					if (error) {
						uni.hideLoading()
						uni.showToast({
							icon: "none",
							title: '网络出错了'

						})
						return Promise.reject('网络出错了');
					}
					if (res.data.code == 200) {
						console.log(11)
						return Promise.resolve(res.data.data);
					}
				})
			},
		}
	}
</script>

<style>
	.info {
		background-color: #034096;
		width: 100%;
		height: 200rpx;
	}

	.infos {
		background-color: #fff;
		height: 200rpx;
		width: 710rpx;
		border-radius: 30rpx;
		margin: 0 auto;
		transform: translateY(50%);
	}

	.one {
		padding: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-weight: 700;
	}

	.address {
		padding: 0 30rpx;
	}

	.list {
		background-color: #fff;

		width: 710rpx;
		border-radius: 30rpx;
		margin: 0 auto;
		transform: translateY(120rpx);
	}

	.list-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx 10rpx;
	}

	.pay {
		border-radius: 30rpx;
		width: 500rpx;
		transform: translateY(140rpx);
		padding: 18rpx;
		text-align: center;
		background-color: #034096;
		color: #fff;
		margin: 30rpx auto;
		font-weight: 700;
	}
</style>